package com.rgs.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.rgs.ssm.domain.Permission;
import com.rgs.ssm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Ren Gaoshuai
 * @date 2021-05-30 14:54
 *
 * 权限  控制器
 **/

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "size",required = true,defaultValue = "5")Integer size){
        ModelAndView mv = new ModelAndView();
        List<Permission> permissions = permissionService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(permissions);
        mv.addObject("permissionList",pageInfo);
        mv.setViewName("permission-list");
        return mv;
    }

    @RequestMapping("/showDetails.do")
    public ModelAndView showDetails(@RequestParam(name = "id",required = true)Integer id){
        ModelAndView mv = new ModelAndView();
        Permission permission = permissionService.findById(id);
        mv.addObject("permission",permission);
        mv.setViewName("permission-show");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(Permission permission){
        permissionService.save(permission);
        return "redirect:/permission/findAll.do";
    }

    @RequestMapping("/deleteById.do")
    public String save(@RequestParam(name = "id",required = true)Integer id){
        permissionService.delete(id);
        return "redirect:/permission/findAll.do";
    }

    @RequestMapping("/toUpdateById.do")
    public ModelAndView toUpdateById(@RequestParam(name = "id",required = true)Integer id){
        ModelAndView mv = new ModelAndView();
        Permission permission = permissionService.findById(id);
        mv.addObject("permission",permission);
        mv.setViewName("permission-update");
        return mv;
    }

    @RequestMapping("/updateById.do")
    public String updateById(Permission permission){
        permissionService.update(permission);
        return "redirect:/permission/findAll.do";
    }
}
